package com.xrui.hbase.codec;

import com.google.common.base.Preconditions;
import com.moz.kiji.delegation.Lookups;
import com.moz.kiji.delegation.NamedProvider;
import com.xrui.hbase.DecodedCell;


import java.io.IOException;

/**
 * Interface for cell encoders.
 * <p>
 * A cell encoder is configured to encode precisely one specific column and knows exactly the type
 * of data it is supposed to encode.
 * <p>
 * HBaseCell encoders are instantiated via {@link CellEncoderFactory}.
 * <p>
 * Intended for developers only.
 */

public interface CellEncoder extends NamedProvider {
    /**
     * Encodes the specified cell.
     *
     * @param cell cell to encode.
     * @return the binary encoding of the cell.
     * @throws IOException on I/O error.
     */
    byte[] encode(DecodedCell<?> cell) throws IOException;

    /**
     * Encodes the specified value.
     *
     * @param cellValue value to encode.
     * @param <T>       type of the value to encode.
     * @return the binary encoding of the cell.
     * @throws IOException on I/O error.
     */
    <T> byte[] encode(T cellValue) throws IOException;

    class Factory {
        /**
         * Creates a new CellEncoder with specified encoder provider name
         *
         * @param name of the encoder
         * @return new instance of the encoder
         */
        public static RowKeyEncoder create(String name) {
            Preconditions.checkNotNull(name);
            return Lookups.getNamed(RowKeyEncoder.class).lookup(name);
        }
    }
}
